<template>
  <view >
    <view class="user-info">
      <view class="user-icon">
        <image :src="imgDetail.user.avatar" mode="widthFix"></image>
      </view>
      <view class="user-desc">
        <view class="user-name">{{imgDetail.user.name}}</view>
        <view class="user-time">{{imgDetail.cTime}}</view>
      </view>
    </view>

    <view class="hight-img">
      <swiper-action @swiper-action='swiperAction'>
      <image :src='imgDetail.thumb'></image>
      </swiper-action>
    </view>

    <view class="user-rank">
      <view class="rank">
        <text class="iconfont icondianzan">{{imgDetail.rank}}</text>
      </view>
      <view class="collect">
        <text class="iconfont iconshoucang">收藏</text>
      </view>
    </view>

    <view class="album-wrap" v-if="album.length">
      <view class="album-title">相关</view>
      <view class="album-group">
        <block v-for="item in album" :key="item.id">
          <view class="album-item">
            <view class="album-img">
              <image :src="item.cover" />
            </view>
            <view class="album-info">
              <view class="album-info-text">专辑</view>
              <view class="album-info-name">{{item.name}}</view>
              <text class="iconfont iconiconfontjiantou4"></text>
            </view>
          </view>
        </block>   
      </view>
    </view>

    <view class="comment hot" v-if="hot.length">
      <view class="comment-title">
        <text class="iconfont iconhot1"></text>
        <text class="comment-text">最热评论</text>
      </view>
      <view class="comment-group">
       <block v-for="item in hot" :key="item.id">
          <view class="comment-item">
          <view class="comment-user">
          <view class="user-icon">
            <image :src="item.user.avatar" mode="widthFix"></image>
          </view>
          <view class="user-name">
             <view>
              <view class="user-nickname">{{item.user.name}}</view>
              <view class="user-time">{{item.atime}}</view>
            </view>
            <view class="user-badge">
              <image v-for="item2 in item.user.title" :key="item2.icon" :src="item2.icon"/>
            </view>
          </view>
          </view>
          <view class="comment-desc">
            <view class="comment-content">{{item.content}}</view>
            <view class="comment-like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
       </block>
      </view>
    </view>

     <view class="comment new" v-if="comment.length">
      <view class="comment-title">
        <text class="iconfont iconpinglun"></text>
        <text class="comment-text">最新评论</text>
      </view>
      <view class="comment-group">
       <block v-for="item in comment" :key="item.id">
          <view class="comment-item">
          <view class="comment-user">
          <view class="user-icon">
            <image :src="item.user.avatar" mode="widthFix"></image>
          </view>
          <view class="user-name">
            <view>
              <view class="user-nickname">{{item.user.name}}</view>
              <view class="user-time">{{item.atime}}</view>
            </view>
            
            <view class="user-badge">
              <image v-for="item2 in item.user.title" :key="item2.icon" :src="item2.icon"/>
            </view>
          </view>
          </view>
          <view class="comment-desc">
            <view class="comment-content">{{item.content}}</view>
            <view class="comment-like">
              <text class="iconfont icondianzan">{{item.size}}</text>
            </view>
          </view>
        </view>
       </block>
      </view>
    </view>

    <view class="download">
      <view class="download-btn" @click="handleImgDownLoad">
        下载图片
      </view>
    </view>
</view>
</template>

<script>
import moment from 'moment'
import {getImgDetail} from '../../network/imgDetail.js'
import swiperAction from '@/components/swiperAction.vue'
export default {
  components: {
    swiperAction
  },
  onLoad() {
    const {imgList,imgIndex} = getApp().globalData;
    this.imgIndex = imgIndex;

    this.imgDetail = imgList[this.imgIndex]
    this.imgDetail.cTime = moment(this.imgDetail.atime*1000).fromNow()
    this.id = this.imgDetail.id;
    this.__getImgDetail()
  },
  data(){
    return {
      imgDetail: {},
      id: '',
      album: [],
      comment: [],
      hot: [],
      // 图片的索引
      imgIndex: 0
    }
  },
  methods: {
    // ================== 发送请求 ==================
     __getImgDetail() {
      wx.request({
        url:`https://service.picasso.adesk.com/v2/wallpaper/wallpaper/${this.id}/comment`,
        success: (res)=> {
          console.log(res)
          this.album = res.data.res.album;
          this.comment = res.data.res.comment;
          this.hot = res.data.res.hot;
        }
      })
    },

    // ================== 事件监听 ==================
    getData() {
      const {imgList} = getApp().globalData;
         this.imgDetail = imgList[this.imgIndex]
        this.imgDetail.cTime = moment(this.imgDetail.atime*1000).fromNow()
        this.id = this.imgDetail.id;
        this.__getImgDetail()
    },
    swiperAction({direction}) {
      const {imgList} = getApp().globalData;
      // let index = this.imgIndex;
      if(direction === 'left'&& this.imgIndex < imgList.length - 1) {
        this.imgIndex++
        
        this.getData()
        
      }else if(direction === 'right'&& this.imgIndex > 0) {
        this.imgIndex--
        

        this.getData()
      }else {
        uni.showToast({
          title: '没有数据了',
          icon: 'none'
        })
      }

     
    },
     async handleImgDownLoad() {
      // 下载图片临时路径
      // await uni.showLoading({title: '下载中'})
     
            uni.showLoading({title: '下载中'})
            
            
            uni.downloadFile({
              url: this.imgDetail.img,
            }).then(result => {
              const {tempFilePath} = result[1]

              uni.saveImageToPhotosAlbum({
                filePath:tempFilePath
              }).then(res => {
                uni.hideLoading()
              })

            })
            uni.showToast({
              title: '下载成功',
              icon: 'success'
            })  
    }
  }
}
</script>

<style scoped lang='scss'>
.user-info {
  padding: 20rpx;
  display: flex;
  .user-icon {
    padding: 0 20rpx;
    image {
      width: 88rpx;
      border-radius: 50%;
    }
  }

  .user-desc {
    .user-name {
      font-weight: 700;
      color:#000;
    }

    .user-time {
      color: #ccc;
      padding: 10rpx 0;
    }
  }
}

.user-rank {
  height: 80rpx;
  display: flex;
  border-bottom: 5rpx solid #eee;
  .rank {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    .iconfont.icondianzan {

    }
  }

  .collect {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    .iconfont.iconshoucang {

    }
  }
}


.album-wrap {
  padding: 20rpx 0;
  .album-title {
    padding: 10rpx 0;
  }

  .album-group {
    .album-item {
      display: flex;
      padding: 10rpx;

      border-bottom: 5rpx solid #ccc;
      .album-img {
        flex: 1;
        image {
          width: 180rpx;
          height: 180rpx;
        }
      }

      .album-info {
        flex:3; 
        padding-left: 20rpx;
        position: relative;
        .album-info-text {
          width: 100rpx;
          height: 50rpx;
          color: #ffffff;
          background-color: $themeColor;
          display: flex;
          justify-content: center;
          align-items: center;
        }
        .iconfont {
          position: absolute;
          top: 50%;
          right: 30rpx;
          transform: translateY(-50%);
        }
        .album-info-name {
          padding: 10rpx 0;
          color: #999;
          font-weight: 700;
        }
      }
    }
  }
}

.comment {
  .comment-title {
    padding: 15rpx;
    .iconfont {
      color: red;
      font-size: 40rpx;
    }

    .comment-text {
      font-weight: 700;
      font-size: 30rpx;
      color: #666;
      margin-left: 10rpx;
    }
  }

  .comment-group {
    .comment-item {
      border-bottom: 15rpx solid #eee;
      .comment-user {
        display: flex;
        padding: 20rpx 0;
        .user-icon {
          width: 15%;
          display: flex;
          justify-content: center;  
          align-items: center;
          image {
            width: 90%;
          }
        }

        .user-name {
          flex: 1;
          display: flex;
          justify-content: space-between;
          .user-nickname {
            color:#777;
          }

          .user-time {
            color:#ccc;
            font-size: 24rpx;
            padding: 5rpx 0;
          }

          .user-badge {
            display: flex;
            image {
              width: 40rpx;
              height: 40rpx;
            }
          }
        }
      }

      .comment-desc {
        display: flex;
        padding: 30rpx 0;
        .comment-content {
          flex: 1;
          padding-left: 15%;
          color: #000;
        }

        .comment-like {
          text-align: right;
          .iconfont {

          }
        }
      }
    }

}
}

.download {
  height: 120rpx;
  display: flex;
  justify-content: center;
  align-items: center;
  .download-btn {
    width: 90%;
    height: 80%;
    background-color: $themeColor;
    font-size: 50rpx;
    font-weight: 600;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    border-radius: 10rpx;
  }
}
</style>